package cn.edu.seu.ssny.controller.admin;

import cn.edu.seu.ssny.common.JsonResult;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.User;
import cn.edu.seu.ssny.service.IEnterpriseService;
import cn.edu.seu.ssny.service.ILoginLogService;
import cn.edu.seu.ssny.service.IRoleService;
import cn.edu.seu.ssny.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController extends BaseController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IEnterpriseService enterpriseService;
	@Autowired
	private ILoginLogService loginLogService;

	@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
	public String login(HttpSession session) {
		if(session.getAttribute("currUser") != null)
			return redirect("/index");
		return "login";
	}
	@RequestMapping(value = { "/login" }, method = RequestMethod.POST)
	public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password, ModelMap model, HttpSession session
			) {
		try {
			 Subject subject = SecurityUtils.getSubject();
			 UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			subject.login(token);
			// session初始化
			subject.getSession().setTimeout(-1000L);

			User currUser = (User) SecurityUtils.getSubject().getPrincipal();
			Enterprise enterprise = enterpriseService.find(1);

			session.setAttribute("enterprise", enterprise);
			session.setAttribute("currUser", currUser);

			if(currUser.getRole().getLevel() != IRoleService.SUPER_LEVEL)
				loginLogService.login(currUser, enterprise);

			return redirect("/index");
		} catch (AuthenticationException e) {
			model.put("message", e.getMessage());
		}
		return "login";
	}
	
	@RequestMapping(value = { "/admin/logout" }, method = RequestMethod.GET)
	public String logout(HttpSession session) {
		User currUser = (User) SecurityUtils.getSubject().getPrincipal();
		if(currUser.getRole().getLevel() != IRoleService.SUPER_LEVEL)
			loginLogService.logout(currUser);

		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return redirect("login");
	}

	@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
	public String register() {
		return "register";
	}

	@RequestMapping(value = { "/register" }, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult register(User user, ModelMap map) {
		Enterprise enterprise = enterpriseService.find(1);
		try {
			userService.register(user, enterprise);
		} catch (Exception e) {
			return JsonResult.failure("用户名已存在");
		}
		return JsonResult.success("注册成功");
	}
	
}
